package com.example.remoteserviceactivity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.servicetest.aidl.ContadorRemoto;

public class RemoteServiceActivity extends Activity implements ServiceConnection {

	protected ContadorRemoto contador;
	protected Handler handler = new Handler();
	
	//// Atualizacao do contador sem thread
	Runnable run = new Runnable() {
		@Override
		public void run() {
			try {
				handler.postDelayed(this, 1000);
				TextView text = (TextView) findViewById(R.id.valorContador);
				text.setText("" + contador.get());
			} catch (RemoteException e) {
			} catch (NullPointerException e) {
			}
		}
	};
	
	// Atualizacao do contador com thread
	class ContadorThread extends Thread {

		public void run() {
			final TextView text = (TextView) findViewById(R.id.valorContador);
			
			Runnable run = new Runnable() {
				@Override
				public void run() {
					try {
						text.setText("" + contador.get());
					} catch (RemoteException e) {
					} catch (NullPointerException e) {
					}
				}
			};

			while (!RemoteServiceActivity.this.isFinishing()) {
				try {
					Thread.sleep(1000);
					handler.post(run);
				} catch (InterruptedException e) { }
			}
			
			Log.i(this.getClass().getCanonicalName(), "Finishing contador thread...");
		}
	};
		
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		final TextView text = (TextView) findViewById(R.id.valorContador);

		Button btnBind = (Button) findViewById(R.id.btnBind);
		btnBind.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// bindService
				Intent intent = new Intent("com.example.servicetest.SERVICE");
				bindService(intent, RemoteServiceActivity.this, BIND_AUTO_CREATE);
			}
		});

		Button btnUnbind = (Button) findViewById(R.id.btnUnbind);
		btnUnbind.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// unbindService
				unbindService(RemoteServiceActivity.this);
			}
		});

		Button btnGet = (Button) findViewById(R.id.btnLerContador);
		btnGet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// ler contador aqui
				int count = 0;
				try {
					count = contador.get();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				text.setText("" + count);
			}
		});

		Button btnReset = (Button) findViewById(R.id.btnZerarContador);
		btnReset.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// zerar contador aqui
				try {
					contador.reset();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		
		// UPGRADE
		new ContadorThread().start();	// Versao com thread
		//handler.postDelayed(run, 1000);	// Versao sem thread
	}


	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		contador = ContadorRemoto.Stub.asInterface(service);
	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
		contador = null;
	}
}
